import { addon } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import { AppServiceUtility } from "src/projects/app/appService";
import { table_param } from "src/projects/app/util-tool";
import { ROUTE_PATH } from "src/projects/router";
import { request } from "src/business/util_tool";
import { Select } from "antd";

let { Option } = Select;
/**
 * 组件：床位页面状态
 */
export interface BedManageViewState extends ReactViewState {
    /** 接口名 */
    request_url?: string;
    /** 区域列表 */
    area_list?: [];
}

/**
 * 组件：床位页面
 * 描述
 */
export class BedManageView extends ReactView<BedManageViewControl, BedManageViewState> {
    private columns_data_source = [{
        title: '区域',
        dataIndex: 'area_name',
        key: 'area_name',
    }, {
        title: '床位名称',
        dataIndex: 'name',
        key: 'name',
    }, {
        title: '床位编号',
        dataIndex: 'bed_code',
        key: 'bed_code',
    }, {
        title: '入住人',
        dataIndex: 'residents',
        key: 'residents',
    }];
    /** 新增按钮 */
    add = () => {
        this.props.history!.push(ROUTE_PATH.changeBedManage);
    }
    /** 自定义图标按钮回调事件 */
    onIconClick = (type: string, contents: any) => {
        if ('icon_edit' === type) {
            console.log('自定义按钮edit返回值：', contents);
            this.props.history!.push(ROUTE_PATH.changeBedManage + '/' + contents.id);
        }
    }
    componentWillMount() {
        let request_url = this.props.request_url;
        this.setState({
            request_url
        });
    }
    componentDidMount() {
        // 查询住宿区域列表
        request(this, AppServiceUtility.hotel_zone_service.get_list!({}, 1, 999))
            .then((datas: any) => {
                this.setState({
                    area_list: datas.result,
                });
            });
    }
    render() {
        let area = this.state.area_list;
        let area_list: any[] = [];
        if (area) {
            area!.map((item, idx) => {
                area_list.push(<Option key={item['id']}>{item['zone_name']}</Option>);
            });
        }
        let service_provider = {
            type_show: false,
            edit_form_items_props: [
                {
                    type: InputType.input,
                    label: "床位名称",
                    decorator_id: "name"
                }, {
                    type: InputType.input,
                    label: "入住人名称",
                    decorator_id: "residents"
                }, {
                    type: InputType.select,
                    label: "所属区域",
                    decorator_id: "area_name",
                    option: {
                        placeholder: "请选择所属区域",
                        childrens: area_list
                    }
                },
            ],
            btn_props: [{
                label: '新增床位',
                btn_method: this.add,
                icon: 'plus'
            }],
            // on_row_selection: this.on_row_selection,
            columns_data_source: this.columns_data_source,
            on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.hotel_zone_type_service,
            service_option: {
                select: {
                    service_func: this.state.request_url,
                    service_condition: [{}]
                },
                delete: {
                    service_func: 'delete_bed'
                }
            },
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.select_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        let service_provider_list = Object.assign(service_provider, table_param);
        return (
            <SignFrameLayout {...service_provider_list} />
        );
    }
}

/**
 * 控件：床位页面
 * 描述
 */
@addon('BedManageView', '床位页面', '描述')
@reactControl(BedManageView, true)
export class BedManageViewControl extends ReactViewControl {
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;
}